package com.app.web.socketio
import com.corundumstudio.socketio.AuthorizationListener
import com.corundumstudio.socketio.HandshakeData
import com.frame.filter.SessionStore
import groovy.transform.CompileStatic
import groovy.transform.TypeChecked
import org.apache.shiro.session.Session
/**
 * Created by three on 14-9-28.
 */
@CompileStatic
@TypeChecked
class SocketIOAuthorizationListener implements AuthorizationListener {
    @Override
    boolean isAuthorized(HandshakeData data) {
        String cookies = data.getHeaders().get("Cookie")
        String[] cookieArray = cookies.substring(1, cookies.length()-1).split(";")
        for(String cookie: cookieArray) {
            String[] kv = cookie.trim().split("=");
            if("JSESSIONID".equals(kv[0])) {
                Session session =  SessionStore.get(kv[1]);
                if(session!=null) {
                    SessionStore.currentSession(session)
                    session.touch()

                    return true;
                }
            }
        }
        return false;
    }
}
